<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            position: relative;
        }
        
        .box {
            position: absolute;
            left: 50%;
            margin-left: -70px;
            top: 100px;
        }
        
        .hour,
        .minute,
        .second {
            display: block;
            width: 50px;
            height: 50px;
            float: left;
            background-color: #333;
            color: #fff;
            text-align: center;
            line-height: 50px;
        }
        
        span {
            float: left;
            height: 50px;
            line-height: 50px;
        }
    </style>
</head>

<body>
    <div class="box">
        <span class="hour">1</span>
        <span>:</span>
        <span class="minute">2</span>
        <span>:</span>

        <span class="second">3</span>
    </div>
    <script>
        var hour = document.querySelector('.hour');
        var minute = document.querySelector('.minute');
        var second = document.querySelector('.second');
        var inputtime = +new Date('2022-5-25 21:00:00')

        function fn() {
            var nowtime = +new Date();
            var times = (inputtime - nowtime) / 1000;
            var s = parseInt(times % 60);
            second.innerHTML = s;
            var m = parseInt(times / 60 % 60);
            minute.innerHTML = m;
            var h = parseInt(times / 60 / 60 % 24);
            hour.innerHTML = h;
        }
        fn(); //在开启定时器之前先调用一次，防止在首次打开时出现123
        setInterval(fn, 1000); //1秒调用一次实现了倒计时效果
    </script>
</body>

</html>